
package com.joysuch.wwyt.audit.support;

import javax.annotation.Resource;

import com.joysuch.wwyt.datasync.support.AopDataSyncDaoInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class RepositoryPostProcessor implements RepositoryProxyPostProcessor {

    private static final Logger logger = LoggerFactory.getLogger(RepositoryPostProcessor.class);

    AopAuditLogDaoInterceptor interceptor = new AopAuditLogDaoInterceptor();

    @Autowired(required = false)
    private AopDataSyncDaoInterceptor dataSyncInterceptor;

    @Value("${wwyt.feature.datasync.enabled}")
    private boolean featureDataSyncEnabled;

    @Override
    public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {
        factory.addAdvice(interceptor);

        // 是否启用数据同步
        if (featureDataSyncEnabled && dataSyncInterceptor != null) {
            logger.debug("Feature DataSync enabled");
            factory.addAdvice(dataSyncInterceptor);
        }
    }

    public boolean isFeatureDataSyncEnabled() {
        return featureDataSyncEnabled;
    }

    public void setFeatureDataSyncEnabled(boolean featureDataSyncEnabled) {
        this.featureDataSyncEnabled = featureDataSyncEnabled;
    }

}
